﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.ETL
{
    /// <summary>
    /// 
    /// </summary>
    public partial class FuzzyLinkEditor : Form, IComponentEditor 
    {
        #region fields
        private object _ActivityNode;
        private FuzzyLinkComponent _FuzzyLinkComponent;
        private Dictionary<string, string> _ComparerName_AssemblyName_Mappings;
        private Dictionary<string, string> _ComparerName_ComparerTypeName_Mappings;
        #endregion

        /// <summary>
        /// 
        /// </summary>
        public FuzzyLinkEditor()
        {
            InitializeComponent();
        }

        private void FuzzyLinkEditor_Load(object sender, EventArgs e)
        {
            this._ComparerName_AssemblyName_Mappings = new Dictionary<string, string>();
            this._ComparerName_ComparerTypeName_Mappings = new Dictionary<string, string>();

            this.cbo_Comparers.Items.Clear();
            Assembly assembly = typeof(IFuzzyFieldComparer).Assembly;
            Type[] types = assembly.GetTypes();
            foreach (Type type in types)
            {
                if (type.GetInterface(typeof(IFuzzyFieldComparer).Name) != null)
                {
                    this._ComparerName_AssemblyName_Mappings.Add(type.Name, assembly.Location);
                    this._ComparerName_ComparerTypeName_Mappings.Add(type.Name, type.FullName);
                    this.cbo_Comparers.Items.Add(type.Name);
                }
            }

            this.cbo_Src1DbProvider.Items.Clear();
            this.cbo_Src2DbProvider.Items.Clear();
            this.cbo_TgtDbProvider.Items.Clear();
            for(int i=0;i<=2;i++)
            {
                DataProviderType dbProvider = (DataProviderType) i;
                this.cbo_Src1DbProvider.Items.Add(dbProvider.ToString());
                this.cbo_Src2DbProvider.Items.Add(dbProvider.ToString());
                this.cbo_TgtDbProvider.Items.Add(dbProvider.ToString());
            }
        }

        #region IComponentEditor Members

        public event UpdateComponentSettings UpdateComponentSettingEvent;

        public object OwnerActivityNode
        {
            get { return _ActivityNode; }
            set { _ActivityNode = value; }
        }

        public Type WorkflowComponentType
        {
            get { return typeof (FuzzyLinkComponent); }
        }

        public void LoadSettings(GlobalVariable[] globalVars, WorkflowComponentBase[] fromComponents,
                                 object activityNode, WorkflowComponentBase wfComponent)
        {
            this._ActivityNode = activityNode;
            this._FuzzyLinkComponent = (FuzzyLinkComponent)wfComponent;

            this.cbo_Src1DbProvider.SelectedIndex = (int) this._FuzzyLinkComponent.SrcDbProvider1;
            this.txt_Src1ConnStr.Text = this._FuzzyLinkComponent.SrcConnStr1;
            this.rt_Src1Sql.Text = this._FuzzyLinkComponent.SrcSql1;
            this.cbo_Src1Key.Items.Clear();
            this.cbo_Src1Field.Items.Clear();
            TableColumn[] cols1 = DbUtil.GetTableColumns(
                this._FuzzyLinkComponent.SrcDbProvider1,
                this._FuzzyLinkComponent.SrcConnStr1,
                this._FuzzyLinkComponent.SrcSql1);
            if(cols1 !=null && cols1.Length>0)
            {
                foreach(TableColumn col in cols1)
                {
                    this.cbo_Src1Key.Items.Add(col.ColumnName);
                    this.cbo_Src1Field.Items.Add(col.ColumnName);
                }
            }
            if(this.cbo_Src1Key.Items.Count>0 && (!string.IsNullOrEmpty(this._FuzzyLinkComponent.SrcKeyField1)))
            {
                for(int i=0;i<this.cbo_Src1Key.Items.Count;i++)
                {
                    if(this.cbo_Src1Key.Items[i].ToString()==this._FuzzyLinkComponent.SrcKeyField1)
                    {
                        this.cbo_Src1Key.SelectedIndex = i;
                        break;
                    }
                }
            }
            if(this.cbo_Src1Field.Items.Count>0 && !string.IsNullOrEmpty(this._FuzzyLinkComponent.SrcField1))
            {
                for(int i=0;i<this.cbo_Src1Field.Items.Count;i++)
                {
                    if(this.cbo_Src1Field.Items[i].ToString()==this._FuzzyLinkComponent.SrcField1)
                    {
                        this.cbo_Src1Field.SelectedIndex = i;
                        break;
                    }
                }
            }

            this.cbo_Src2DbProvider.SelectedIndex = (int) this._FuzzyLinkComponent.SrcDbProvider2;
            this.txt_Src2ConnStr.Text = this._FuzzyLinkComponent.SrcConnStr2;
            this.rt_Src2Sql.Text = this._FuzzyLinkComponent.SrcSql2;
            TableColumn[] cols2 = DbUtil.GetTableColumns(
                this._FuzzyLinkComponent.SrcDbProvider2,
                this._FuzzyLinkComponent.SrcConnStr2,
                this._FuzzyLinkComponent.SrcSql2);
            this.cbo_Src2Key.Items.Clear();
            this.cbo_Src2Field.Items.Clear();
            if(cols2!=null && cols2.Length>0)
            {
                foreach(TableColumn col in cols2)
                {
                    this.cbo_Src2Key.Items.Add(col.ColumnName);
                    this.cbo_Src2Field.Items.Add(col.ColumnName);
                }
            }
            if(this.cbo_Src2Key.Items.Count>0 && !string.IsNullOrEmpty(this._FuzzyLinkComponent.SrcKeyField2))
            {
                for(int i=0;i<this.cbo_Src2Key.Items.Count;i++)
                {
                    if(this.cbo_Src2Key.Items[i].ToString()==this._FuzzyLinkComponent.SrcKeyField2)
                    {
                        this.cbo_Src2Key.SelectedIndex = i;
                        break;
                    }
                }
            }
            if (this.cbo_Src2Field.Items.Count > 0 && !string.IsNullOrEmpty(this._FuzzyLinkComponent.SrcField2))
            {
                for(int i=0;i<this.cbo_Src2Field.Items.Count;i++)
                {
                    if(this.cbo_Src2Field.Items[i].ToString()==this._FuzzyLinkComponent.SrcField2)
                    {
                        this.cbo_Src2Field.SelectedIndex = i;
                        break;
                    }
                }
            }

            this.cbo_TgtDbProvider.SelectedIndex = (int)this._FuzzyLinkComponent.TgtDbProvider;
            this.txt_TgtConnStr.Text = this._FuzzyLinkComponent.TgtConnStr;
            if(this.cbo_Comparers.Items.Count>0 && !string.IsNullOrEmpty(this._FuzzyLinkComponent.ComparerTypeName))
            {
                for(int i=0;i<this.cbo_Comparers.Items.Count;i++)
                {
                    string comparerName = this.cbo_Comparers.Items[i].ToString();
                    if(this._ComparerName_ComparerTypeName_Mappings.ContainsKey(comparerName) && 
                        this._ComparerName_ComparerTypeName_Mappings[comparerName]==this._FuzzyLinkComponent.ComparerTypeName)
                    {
                        this.cbo_Comparers.SelectedIndex = i;
                        break;
                    }
                }
            }
            this.num_Threshold.Value = (decimal)this._FuzzyLinkComponent.FieldSimilarityScoreThreshold;

            this.dv_FieldMappings.Rows.Clear();
            string[] tblNames = DbUtil.GetTableNames(this._FuzzyLinkComponent.TgtDbProvider,
                                                     string.Empty, this._FuzzyLinkComponent.TgtConnStr);
            this.cbo_TgtTable.Items.Clear();
            if(tblNames !=null && tblNames.Length>0)
            {
                foreach(string tblname in tblNames)
                {
                    this.cbo_TgtTable.Items.Add(tblname);
                }
            }
            if(this.cbo_TgtTable.Items.Count>0 && 
                !string.IsNullOrEmpty(this._FuzzyLinkComponent.TgtTableName))
            {
                for(int i=0;i<this.cbo_TgtTable.Items.Count;i++)
                {
                    if(this.cbo_TgtTable.Items[i].ToString()==this._FuzzyLinkComponent.TgtTableName)
                    {
                        this.cbo_TgtTable.SelectedIndex = i;
                        break;
                    }
                }
            }

            if(this.cbo_LinkScoreField.Items.Count>0 && !string.IsNullOrEmpty(this._FuzzyLinkComponent.LinkScoreFieldInTargetTable))
            {
                for(int i=0;i<this.cbo_LinkScoreField.Items.Count;i++)
                {
                    if(this.cbo_LinkScoreField.Items[i].ToString()==this._FuzzyLinkComponent.LinkScoreFieldInTargetTable)
                    {
                        this.cbo_LinkScoreField.SelectedIndex = i;
                        break;
                    }
                }
            }

            TableColumn[] tgtCols = DbUtil.GetTableColumns(
                this._FuzzyLinkComponent.TgtDbProvider, this._FuzzyLinkComponent.TgtConnStr,
                string.Empty, this._FuzzyLinkComponent.TgtTableName);

            if(this._FuzzyLinkComponent.Src1TgtFieldMappings != null && this._FuzzyLinkComponent.Src1TgtFieldMappings.Count>0)
            {
                foreach(string srcFieldName in this._FuzzyLinkComponent.Src1TgtFieldMappings.Keys)
                {
                    DataGridViewRow dr = new DataGridViewRow();
                    DataGridViewComboBoxCell cellTbl = new DataGridViewComboBoxCell();
                    cellTbl.Items.Add("Source 1");
                    cellTbl.Items.Add("Source 2");
                    cellTbl.Value = "Source 1";
                    dr.Cells.Add(cellTbl);
                    DataGridViewTextBoxCell cellSrc = new DataGridViewTextBoxCell();
                    cellSrc.Value = srcFieldName;
                    dr.Cells.Add(cellSrc);
                    DataGridViewComboBoxCell cellTgt = new DataGridViewComboBoxCell();
                    if(tgtCols !=null && tgtCols.Length>0)
                    {
                        foreach(TableColumn col in tgtCols)
                        {
                            cellTgt.Items.Add(col.ColumnName);
                            if(this._FuzzyLinkComponent.Src1TgtFieldMappings!=null && 
                                this._FuzzyLinkComponent.Src1TgtFieldMappings.ContainsKey(srcFieldName) &&
                                this._FuzzyLinkComponent.Src1TgtFieldMappings[srcFieldName]!=null)
                            {
                                if (col.ColumnName.ToLower() == this._FuzzyLinkComponent.Src1TgtFieldMappings[srcFieldName].ToLower())
                                {
                                    cellTgt.Value = this._FuzzyLinkComponent.Src1TgtFieldMappings[srcFieldName];
                                }    
                            }
                            
                        }
                    }
                    dr.Cells.Add(cellTgt);
                    this.dv_FieldMappings.Rows.Add(dr);
                }
            }
            if (this._FuzzyLinkComponent.Src2TgtFieldMappings != null && this._FuzzyLinkComponent.Src2TgtFieldMappings.Count > 0)
            {
                foreach (string srcFieldName in this._FuzzyLinkComponent.Src2TgtFieldMappings.Keys)
                {
                    DataGridViewRow dr = new DataGridViewRow();
                    DataGridViewComboBoxCell cellTbl = new DataGridViewComboBoxCell();
                    cellTbl.Items.Add("Source 1");
                    cellTbl.Items.Add("Source 2");
                    cellTbl.Value = "Source 2";
                    dr.Cells.Add(cellTbl);
                    DataGridViewTextBoxCell cellSrc = new DataGridViewTextBoxCell();
                    cellSrc.Value = srcFieldName;
                    dr.Cells.Add(cellSrc);
                    DataGridViewComboBoxCell cellTgt = new DataGridViewComboBoxCell();
                    if (tgtCols != null && tgtCols.Length > 0)
                    {
                        foreach (TableColumn col in tgtCols)
                        {
                            cellTgt.Items.Add(col.ColumnName);
                            if (this._FuzzyLinkComponent.Src2TgtFieldMappings != null &&
                                this._FuzzyLinkComponent.Src2TgtFieldMappings.ContainsKey(srcFieldName) &&
                                this._FuzzyLinkComponent.Src2TgtFieldMappings[srcFieldName] != null)
                            {
                                if (col.ColumnName.ToLower() ==
                                    this._FuzzyLinkComponent.Src2TgtFieldMappings[srcFieldName].ToLower())
                                {
                                    cellTgt.Value = this._FuzzyLinkComponent.Src2TgtFieldMappings[srcFieldName];
                                }
                            }
                        }
                    }
                    dr.Cells.Add(cellTgt);
                    this.dv_FieldMappings.Rows.Add(dr);
                }
            }

            string joinSql = string.Empty;
            if(this._FuzzyLinkComponent.Src1_Src2_Joinings !=null && this._FuzzyLinkComponent.Src1_Src2_Joinings.Count>0)
            {
                string[] joinFields1 = new string[this._FuzzyLinkComponent.Src1_Src2_Joinings.Count];
                this._FuzzyLinkComponent.Src1_Src2_Joinings.Keys.CopyTo(joinFields1, 0);
                for(int i=0;i<joinFields1.Length;i++)
                {
                    joinSql += joinFields1[i] + "=" + this._FuzzyLinkComponent.Src1_Src2_Joinings[joinFields1[i]];
                    if (i < joinSql.Length - 1)
                        joinSql += " and ";
                }
            }
            this.rt_Join.Text = joinSql;

            this.ck_SkipUpdateExistingData.Checked = this._FuzzyLinkComponent.SkipUpdateExistingData;
        }

        public void SaveSettings()
        {
            this._FuzzyLinkComponent.SrcDbProvider1 = (DataProviderType) this.cbo_Src1DbProvider.SelectedIndex;
            this._FuzzyLinkComponent.SrcConnStr1 = this.txt_Src1ConnStr.Text;
            this._FuzzyLinkComponent.SrcSql1 = this.rt_Src1Sql.Text;
            this._FuzzyLinkComponent.SrcKeyField1 = this.cbo_Src1Key.Text;
            this._FuzzyLinkComponent.SrcField1 = this.cbo_Src1Field.Text;

            this._FuzzyLinkComponent.SrcDbProvider2 = (DataProviderType)this.cbo_Src2DbProvider.SelectedIndex;
            this._FuzzyLinkComponent.SrcConnStr2 = this.txt_Src2ConnStr.Text;
            this._FuzzyLinkComponent.SrcSql2 = this.rt_Src2Sql.Text;
            this._FuzzyLinkComponent.SrcKeyField2 = this.cbo_Src2Key.Text;
            this._FuzzyLinkComponent.SrcField2 = this.cbo_Src2Field.Text;

            this._FuzzyLinkComponent.TgtDbProvider = (DataProviderType)this.cbo_TgtDbProvider.SelectedIndex;
            this._FuzzyLinkComponent.TgtConnStr = this.txt_TgtConnStr.Text;
            this._FuzzyLinkComponent.TgtTableName = this.cbo_TgtTable.Text;

            this._FuzzyLinkComponent.ComparerAssemblyName = string.Empty;
            this._FuzzyLinkComponent.ComparerTypeName = string.Empty;
            if(this.cbo_Comparers.SelectedIndex>=0)
            {
                if (this._ComparerName_AssemblyName_Mappings.ContainsKey(this.cbo_Comparers.SelectedItem.ToString()))
                    this._FuzzyLinkComponent.ComparerAssemblyName =
                        this._ComparerName_AssemblyName_Mappings[this.cbo_Comparers.SelectedItem.ToString()];
                if (this._ComparerName_ComparerTypeName_Mappings.ContainsKey(this.cbo_Comparers.SelectedItem.ToString()))
                    this._FuzzyLinkComponent.ComparerTypeName =
                        this._ComparerName_ComparerTypeName_Mappings[this.cbo_Comparers.SelectedItem.ToString()];
            }
            this._FuzzyLinkComponent.FieldSimilarityScoreThreshold = (double)this.num_Threshold.Value;

            this._FuzzyLinkComponent.Src1TgtFieldMappings = new Dictionary<string, string>();
            this._FuzzyLinkComponent.Src2TgtFieldMappings = new Dictionary<string, string>();
            foreach(DataGridViewRow dr in this.dv_FieldMappings.Rows)
            {
                if(! dr.IsNewRow && dr.Cells[0].Value !=null && dr.Cells[1].Value !=null)
                {
                    string srcTbl = (string) dr.Cells[0].Value;
                    string srcField = (string)dr.Cells[1].Value;
                    string tgtField = (string) dr.Cells[2].Value;
                    if(srcTbl=="Source 1" && 
                        !this._FuzzyLinkComponent.Src1TgtFieldMappings.ContainsKey(srcField))
                    {
                        this._FuzzyLinkComponent.Src1TgtFieldMappings.Add(srcField, tgtField);
                    }
                    else if(srcTbl=="Source 2" && 
                        !this._FuzzyLinkComponent.Src2TgtFieldMappings.ContainsKey(srcField))
                    {
                        this._FuzzyLinkComponent.Src2TgtFieldMappings.Add(srcField, tgtField);
                    }
                }
            }

            this._FuzzyLinkComponent.Src1_Src2_Joinings = new Dictionary<string, string>();
            if(this.rt_Join.Text.Length>0)
            {
                string[] joinFields = StringUtil.Split(this.rt_Join.Text, " and ");
                foreach(string join in joinFields)
                {
                    string[] pair = StringUtil.Split(join, "=");
                    if(pair.Length==2)
                    {
                        string joinSrc1 = pair[0].Trim();
                        string joinSrc2 = pair[1].Trim();
                        if (!this._FuzzyLinkComponent.Src1_Src2_Joinings.ContainsKey(joinSrc1))
                            this._FuzzyLinkComponent.Src1_Src2_Joinings.Add(joinSrc1, joinSrc2);
                    }
                }
            }

            this._FuzzyLinkComponent.LinkScoreFieldInTargetTable = this.cbo_LinkScoreField.Text;

            this._FuzzyLinkComponent.SkipUpdateExistingData = this.ck_SkipUpdateExistingData.Checked;
        }

        public TestResult Test()
        {
            throw new NotImplementedException();
        }

        #endregion

        private void btn_RefreshTgt_Click(object sender, EventArgs e)
        {
            if(!string.IsNullOrEmpty(this.txt_TgtConnStr.Text))
            {
                DataProviderType dbProvider = DataProviderType.MSSQL;
                if (this.cbo_TgtDbProvider.SelectedIndex == 1)
                    dbProvider = DataProviderType.Oracle;
                string[] tblnames = DbUtil.GetTableNames(dbProvider, string.Empty, this.txt_TgtConnStr.Text);
                this.cbo_TgtTable.Items.Clear();
                if(tblnames !=null && tblnames.Length>0)
                {
                    foreach(string tblName in tblnames)
                    {
                        this.cbo_TgtTable.Items.Add(tblName);
                    }
                }
            }
        }

        private void btn_RefreshSrc2_Click(object sender, EventArgs e)
        {
            DataProviderType dbProvider = DataProviderType.MSSQL;
            if (this.cbo_Src2DbProvider.SelectedIndex == 1)
                dbProvider = DataProviderType.Oracle;
            TableColumn[] cols = DbUtil.GetTableColumns(
                dbProvider, this.txt_Src2ConnStr.Text, this.rt_Src2Sql.Text);
            this.cbo_Src2Field.Items.Clear();
            this.cbo_Src2Key.Items.Clear();
            this.cbo_Src2Join.Items.Clear();

            if (cols != null && cols.Length > 0)
            {
                foreach (TableColumn col in cols)
                {
                    this.cbo_Src2Key.Items.Add(col.ColumnName);
                    this.cbo_Src2Field.Items.Add(col.ColumnName);
                    this.cbo_Src2Join.Items.Add(col.ColumnName);
                }

                if (!string.IsNullOrEmpty(this._FuzzyLinkComponent.SrcField2))
                {
                    for (int i = 0; i < this.cbo_Src2Field.Items.Count; i++)
                    {
                        if (this.cbo_Src2Field.Items[i].ToString() == this._FuzzyLinkComponent.SrcField2)
                        {
                            this.cbo_Src2Field.SelectedIndex = i;
                            break;
                        }
                    }
                }
                if (!string.IsNullOrEmpty(this._FuzzyLinkComponent.SrcKeyField2))
                {
                    for (int i = 0; i < this.cbo_Src2Key.Items.Count; i++)
                    {
                        if (this.cbo_Src2Key.Items[i].ToString() == this._FuzzyLinkComponent.SrcKeyField2)
                        {
                            this.cbo_Src2Key.SelectedIndex = i;
                            break;
                        }
                    }
                }
            }
        }

        private void btn_RefreshSrc1_Click(object sender, EventArgs e)
        {
            DataProviderType dbProvider = DataProviderType.MSSQL;
            if (this.cbo_Src1DbProvider.SelectedIndex == 1)
                dbProvider = DataProviderType.Oracle;
            TableColumn[] cols = DbUtil.GetTableColumns(
                dbProvider, this.txt_Src1ConnStr.Text, this.rt_Src1Sql.Text);
            this.cbo_Src1Field.Items.Clear();
            this.cbo_Src1Key.Items.Clear();
            this.cbo_Src1Join.Items.Clear();

            if(cols !=null && cols.Length>0)
            {
                foreach(TableColumn col in cols)
                {
                    this.cbo_Src1Key.Items.Add(col.ColumnName);
                    this.cbo_Src1Field.Items.Add(col.ColumnName);
                    this.cbo_Src1Join.Items.Add(col.ColumnName);
                }

                if(!string.IsNullOrEmpty(this._FuzzyLinkComponent.SrcField1))
                {
                    for(int i=0;i<this.cbo_Src1Field.Items.Count;i++)
                    {
                        if(this.cbo_Src1Field.Items[i].ToString()==this._FuzzyLinkComponent.SrcField1)
                        {
                            this.cbo_Src1Field.SelectedIndex = i;
                            break;
                        }
                    }
                }
                if(!string.IsNullOrEmpty(this._FuzzyLinkComponent.SrcKeyField1))
                {
                    for(int i=0;i<this.cbo_Src1Key.Items.Count;i++)
                    {
                        if(this.cbo_Src1Key.Items[i].ToString()==this._FuzzyLinkComponent.SrcKeyField1)
                        {
                            this.cbo_Src1Key.SelectedIndex = i;
                            break;
                        }
                    }
                }
            }

        }

        private void dv_FieldMappings_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            
            foreach(DataGridViewRow dr in this.dv_FieldMappings.Rows)
            {
                if (dr.IsNewRow)
                {
                    if (!string.IsNullOrEmpty(this.txt_TgtConnStr.Text) && 
                        !string.IsNullOrEmpty(this.cbo_TgtTable.Text))
                    {
                        DataProviderType dbProvider = DataProviderType.MSSQL;
                        if (this.cbo_TgtDbProvider.SelectedIndex == 1)
                            dbProvider = DataProviderType.Oracle;
                        TableColumn[] tgtCols = DbUtil.GetTableColumns(
                            dbProvider, this.txt_TgtConnStr.Text, string.Empty, this.cbo_TgtTable.Text);
                        DataGridViewComboBoxCell cellTgt = (DataGridViewComboBoxCell)dr.Cells[2];
                        cellTgt.Items.Clear();
                        if (tgtCols != null && tgtCols.Length > 0)
                        {
                            foreach (TableColumn col in tgtCols)
                            {
                                cellTgt.Items.Add(col.ColumnName);
                            }
                        }
                    }

                    DataGridViewComboBoxCell cellSrcTbl = (DataGridViewComboBoxCell) dr.Cells[0];
                    cellSrcTbl.Items.Clear();
                    cellSrcTbl.Items.Add("Source 1");
                    cellSrcTbl.Items.Add("Source 2");
                }
            }
        }

        private void cbo_TgtTable_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(this.cbo_TgtTable.SelectedIndex>=0)
            {
                DataProviderType dbProvider = DataProviderType.MSSQL;
                if (this.cbo_TgtDbProvider.SelectedIndex == 1)
                    dbProvider = DataProviderType.Oracle;
                TableColumn[] tgtCols = DbUtil.GetTableColumns(
                    dbProvider, this.txt_TgtConnStr.Text, string.Empty, this.cbo_TgtTable.Text);

                this.cbo_LinkScoreField.Items.Clear();
                if(tgtCols !=null && tgtCols.Length>0)
                {
                    foreach(TableColumn col in tgtCols)
                    {
                        this.cbo_LinkScoreField.Items.Add(col.ColumnName);
                    }
                }

                foreach (DataGridViewRow dr in this.dv_FieldMappings.Rows)
                {
                    if (dr.IsNewRow)
                    {
                        DataGridViewComboBoxCell cellTgt = (DataGridViewComboBoxCell)dr.Cells[2];
                        cellTgt.Items.Clear();
                        if (tgtCols != null && tgtCols.Length > 0)
                        {
                            foreach (TableColumn col in tgtCols)
                            {
                                cellTgt.Items.Add(col.ColumnName);
                            }
                        }

                        DataGridViewComboBoxCell cellSrcTbl = (DataGridViewComboBoxCell) dr.Cells[0];
                        cellSrcTbl.Items.Clear();
                        cellSrcTbl.Items.Add("Source 1");
                        cellSrcTbl.Items.Add("Source 2");
                    }
                }
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            this.SaveSettings();

            if (this.UpdateComponentSettingEvent != null)
                this.UpdateComponentSettingEvent(this._ActivityNode, this._FuzzyLinkComponent);

            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_AddJoinField_Click(object sender, EventArgs e)
        {
            if(this.cbo_Src1Join.SelectedIndex>=0 && this.cbo_Src2Join.SelectedIndex>=0)
            {
                string join = this.cbo_Src1Join.SelectedItem.ToString() + "=" +
                              this.cbo_Src2Join.SelectedItem.ToString();
                if (this.rt_Join.Text.Length > 0)
                    rt_Join.Text = rt_Join.Text + " and " + join;
                else
                    rt_Join.Text = join;
            }
        }

    }
}
